package com.esms.controller;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpSession;

import com.esms.service.ILogService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.esms.dao.EmployeeMapper;
import com.esms.po.Employee;
import com.esms.po.Grand;
import com.esms.po.Tassessment;
import com.esms.po.Xevaluation;
import com.esms.service.impl.EmployeeXevaluationServerImpl;
import com.esms.vo.EmployeeXevaluationVO;
import com.fasterxml.jackson.databind.JavaType;
import com.fasterxml.jackson.databind.ObjectMapper;

/**
 * 
 * @author 帅帅帅帅
 *
 */

@Controller
public class EmployeeXevaluationController {

	@Autowired
	private ILogService logService;
	
	@Autowired
	private EmployeeXevaluationServerImpl employeeXevaluationService;
	
	@Autowired
    private EmployeeMapper employeeMapper = null;
	
    @RequestMapping("employeeXevaluation.do")
    @ResponseBody
    public EmployeeXevaluationVO findSelectiveGrand(
            @RequestParam(value="e_id", defaultValue="0") String fEid,
            HttpSession httpSession) throws Exception {

        Integer eId = (Integer) httpSession.getAttribute("employeeId");
        //pageNum:起始页面  pageSize:每页的大小
      //  PageHelper.startPage(pageNum,limit);
        //查找条件，一定要紧跟在startPage后
        
        List<Xevaluation> xevaluationsList = employeeXevaluationService.getEmployeeXevaluationList(eId,fEid);

        //设置前台需要的数据
        EmployeeXevaluationVO employeeXevaluationVO = new EmployeeXevaluationVO();
        employeeXevaluationVO.setCode(0);
        employeeXevaluationVO.setMsg("");
        employeeXevaluationVO.setCount(xevaluationsList.size());
        employeeXevaluationVO.setData(xevaluationsList);
        return employeeXevaluationVO;
    }
    
    @RequestMapping("updateXevaluation.do")
    @ResponseBody
    public EmployeeXevaluationVO updateXevaluation(
    		@RequestParam("list") String list,
            HttpSession httpSession) throws Exception {
    	int req = 0;
    	String reMsg;
    	ObjectMapper mapper = new ObjectMapper();
    	JavaType jt = mapper.getTypeFactory().constructParametricType(ArrayList.class, Xevaluation.class);
    	List<Xevaluation> listXevaluation =  (List<Xevaluation>)mapper.readValue(list, jt);
    	/*我的工号查询*/
    	Integer eId = (Integer) httpSession.getAttribute("employeeId");
    	Employee employee = employeeMapper.selectByPrimaryKey(eId);
    	reMsg = "评价成功";
		logService.addLog(this.getClass().getName(),Thread.currentThread().getStackTrace()[1].getMethodName(),(Integer)httpSession.getAttribute("employeeId"),
				"上级对下级评价成功");
    	String quarter = null;
        String systemTime = new SimpleDateFormat("yyyyMMddHHmmss").format(new Date(System.currentTimeMillis()));
        int year = Calendar.getInstance().get(Calendar.YEAR);
        int month = Calendar.getInstance().get(Calendar.MONTH)+1;
        if(month>=1 && month<=3) {
        	quarter = year + "01";
        }else if (month>=4 && month<=6) {
        	quarter = year + "02";
        }else if (month>=7 && month<=9) {
        	quarter = year + "03";
        }else if (month>=10 && month<=12) {
        	quarter = year + "04";
        }
        Tassessment tassessment = new Tassessment();
    	for(int i=0;i<listXevaluation.size();i++) {
    		Xevaluation xevaluation = listXevaluation.get(i);
    		/*输入分数判断*/
    		
    		/*设置同级评分表*/
    		tassessment.settAuserid(employee.geteAccount());
    		tassessment.settDuserid(xevaluation.geteAccount());
    		tassessment.settCreateTime(systemTime);
    		tassessment.settCreateQuarter(quarter);
    		tassessment.settTargetid1(1);
    		tassessment.settTargetid2(2);
    		tassessment.settTargetid3(3);
    		tassessment.settTargetid4(4);
    		tassessment.settTargetid5(5);
    		tassessment.settTargetid6(6);
    		tassessment.settTargetid7(7);
    		tassessment.settTargetid8(8);
    		tassessment.settTargetid9(9);
    		tassessment.settTargetid10(10);
    		tassessment.settTargetid11(11);
    		tassessment.settTargetid12(12);
    		tassessment.settTargetid13(13);
    		tassessment.settTargetid14(14);
    		tassessment.settTargetid15(15);
    		tassessment.settTargetid16(16);
    		tassessment.settTargetid17(17);
    		tassessment.settTargetid18(18);
    		tassessment.settTargetid19(19);
    		tassessment.settTargetid20(20);
    		tassessment.settTargetscore1(xevaluation.gettTargetscore1());
    		tassessment.settTargetscore2(xevaluation.gettTargetscore2());
    		tassessment.settTargetscore3(xevaluation.gettTargetscore3());
    		tassessment.settTargetscore4(xevaluation.gettTargetscore4());
    		tassessment.settTargetscore5(xevaluation.gettTargetscore5());
    		tassessment.settTargetscore6(xevaluation.gettTargetscore6());
    		tassessment.settTargetscore7(xevaluation.gettTargetscore7());
    		tassessment.settTargetscore8(xevaluation.gettTargetscore8());
    		tassessment.settTargetscore9(xevaluation.gettTargetscore9());
    		tassessment.settTargetscore10(xevaluation.gettTargetscore10());
    		tassessment.settTargetscore11(xevaluation.gettTargetscore11());
    		tassessment.settTargetscore12(xevaluation.gettTargetscore12());
    		tassessment.settTargetscore13(xevaluation.gettTargetscore13());
    		tassessment.settTargetscore14(xevaluation.gettTargetscore14());
    		tassessment.settTargetscore15(xevaluation.gettTargetscore15());
    		tassessment.settTargetscore16(xevaluation.gettTargetscore16());
    		tassessment.settTargetscore17(xevaluation.gettTargetscore17());
    		tassessment.settTargetscore18(xevaluation.gettTargetscore18());
    		tassessment.settTargetscore19(xevaluation.gettTargetscore19());
    		tassessment.settTargetscore20(xevaluation.gettTargetscore20());
    		int sum = xevaluation.gettTargetscore1() + xevaluation.gettTargetscore2()
    		 + xevaluation.gettTargetscore3() + xevaluation.gettTargetscore4()
    		 + xevaluation.gettTargetscore5() + xevaluation.gettTargetscore6() 
    		 + xevaluation.gettTargetscore7() + xevaluation.gettTargetscore8()
    		 + xevaluation.gettTargetscore9() + xevaluation.gettTargetscore10()
    		 + xevaluation.gettTargetscore11() + xevaluation.gettTargetscore12()
    		 + xevaluation.gettTargetscore13() + xevaluation.gettTargetscore14()
    		 + xevaluation.gettTargetscore15() + xevaluation.gettTargetscore16()
    		 + xevaluation.gettTargetscore17() + xevaluation.gettTargetscore18()
    		 + xevaluation.gettTargetscore19() + xevaluation.gettTargetscore20();
    		tassessment.settTargetscoresum(sum);
    		
    		req = employeeXevaluationService.deleteXevaluation(employee.geteAccount(),xevaluation.geteAccount());
    		if(req < 0) {
       		 reMsg = "评价失败";
				logService.addLog(this.getClass().getName(),Thread.currentThread().getStackTrace()[1].getMethodName(),(Integer)httpSession.getAttribute("employeeId"),
						"上级对下级评价失败");
    		}
    		req = employeeXevaluationService.insertXevaluation(tassessment);
    		if(req < 0) {
          		 reMsg = "评价失败";
				logService.addLog(this.getClass().getName(),Thread.currentThread().getStackTrace()[1].getMethodName(),(Integer)httpSession.getAttribute("employeeId"),
						"上级对下级评价失败");
       		}
        }

        //设置前台需要的数据
    	EmployeeXevaluationVO employeeXevaluationVO = new EmployeeXevaluationVO();
    	employeeXevaluationVO.setCode(0);
    	employeeXevaluationVO.setMsg(reMsg);
    	employeeXevaluationVO.setData(listXevaluation);
        return employeeXevaluationVO;
    }
    public int checkNumber(Grand grand) {
    	return 0;
    }
}
